package com.medical.admin.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.medical.admin.sys.common.dto.TitleDto;
import com.medical.admin.sys.entity.Title;
import com.medical.admin.sys.mapper.TitleMapper;
import com.medical.admin.sys.service.TitleService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.uitils.BeanPageUtils;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Supplier;

/*
 * @Date : 2024/4/28 上午10:18
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Service
public class TitleServiceImpl implements TitleService {

    @Resource
    private TitleMapper titleMapper;

    @Override
    public Integer add(TitleDto titleDto) {
        Title title = BeanConvertUtils.convertTo(titleDto, Title::new);
        if (ObjectUtil.isNotEmpty(titleMapper.selectEqName(title))) {
            throw new ServiceException(ResponseCode.TITLE_IS_HAVE);
        }
        return titleMapper.insert(title);
    }

    @Override
    public Integer mod(TitleDto titleDto) {
        Title title = BeanConvertUtils.convertTo(titleDto, Title::new);
        if (ObjectUtil.isNotEmpty(titleMapper.selectEqNameNoSelf(title))) {
            throw new ServiceException(ResponseCode.TITLE_IS_HAVE);
        }
        return titleMapper.updateById(title);
    }

    @Override
    public Integer modStatus(Integer delId,Integer titleStatus) {
        return titleMapper.del(delId,titleStatus);
    }

    @Override
    public Page<TitleDto> list(Integer pageNum, Integer pageSize, String titleName) {
        LambdaQueryWrapper<Title> lambda = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(titleName)) {
            lambda.like(Title::getMarTitleName,titleName);
        }

        Page<Title> page = new Page<>(pageNum,pageSize);
        Page<Title> titlePage = titleMapper.selectPage(page, lambda);
        Supplier<TitleDto> targetSupplier = TitleDto::new;
        return BeanPageUtils.convertPage(titlePage, new PageDTO<>(), targetSupplier);
    }

    @Override
    public Integer del(Integer delId) {
        LambdaUpdateWrapper<Title> eq = new LambdaUpdateWrapper<Title>().set(Title::getMarTitleStatus, 0).eq(Title::getTitleId, delId);
        return titleMapper.update(eq);
    }

    @Override
    public Integer delBatch(List<Integer> delIds) {
        return titleMapper.delBatch(delIds);
    }

    @Override
    public TitleDto selectById(Integer titleId) {
        Title title = titleMapper.selectById(titleId);
        return BeanConvertUtils.convertTo(title, TitleDto::new);
    }
}
